﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logistics.DataManage.DAL;
using Logistics.DataManage;

namespace Logistics
{
    public partial class RecordEditWindow
    {
        private const String CONST_Bank_NONE_KEY = "";
        private const String CONST_Bank_NONE_VALUE = "请选择";
        private const String CONST_Bank_ICBC = "工行";
        private const String CONST_Bank_ABC = "农行";
        private const String CONST_Bank_CCB = "建行";
        private const String CONST_Bank_CMB = "招行";
        private const String CONST_Window_Station_SRC = "源站点收";
        private const String CONST_Window_Station_DES = "目的站收";
        private const String CONST_Window_Station_SRC_KEY = "1";
        private const String CONST_Window_Station_DES_KEY = "2";

        private const String CONST_PAYMENT_Type_PAYLATER = "提付";
        private const String CONST_PAYMENT_Type_PAYNOW = "现付";
        private const String CONST_PAYMENT_Type_PAYLATER_KEY = "1";
        private const String CONST_PAYMENT_Type_PAYNOW_KEY = "2";

        private const String CONST_DeliveryServiceMode_Delivers = "送货上门";
        private const String CONST_DeliveryServiceMode_SelfCollection = "自提";
        private static String[] CONST_BackTypes = { "签发收条", "签发盖章", "身份号码" };

        private BillDataInfo CreateBill()
        {
            BillDataInfo Bill = new BillDataInfo();
            IDictionary<String, String> BankList = new Dictionary<String, String>();
            BankList.Add(CONST_Bank_NONE_KEY, CONST_Bank_NONE_VALUE);
            BankList.Add(CONST_Bank_ICBC, CONST_Bank_ICBC);
            BankList.Add(CONST_Bank_CCB, CONST_Bank_CCB);
            BankList.Add(CONST_Bank_ABC, CONST_Bank_ABC);
            BankList.Add(CONST_Bank_CMB, CONST_Bank_CMB);
            Bill.VABankList = BankList;
            Bill.VABank = CONST_Bank_NONE_KEY;

            IDictionary<String, String> PaymentWindowList = new Dictionary<String, String>();
            PaymentWindowList.Add(CONST_Window_Station_SRC_KEY, CONST_Window_Station_SRC);
            PaymentWindowList.Add(CONST_Window_Station_DES_KEY, CONST_Window_Station_DES);
            Bill.PaymentWindowList = PaymentWindowList;

            IDictionary<String, String> PayTypeList = new Dictionary<String, String>();
            PayTypeList.Add(CONST_PAYMENT_Type_PAYLATER_KEY, CONST_PAYMENT_Type_PAYLATER);
            PayTypeList.Add(CONST_PAYMENT_Type_PAYNOW_KEY, CONST_PAYMENT_Type_PAYNOW);
            Bill.PayTypeList = PayTypeList;
            Bill.PayType = PayTypeList.Keys.First();

            Bill.VABank = CONST_Bank_NONE_KEY;
            Bill.VAInsuranceFeeWindow = CONST_Window_Station_SRC_KEY;
            Bill.VADeliveryFeeWindow = CONST_Window_Station_DES_KEY;

            Bill.CreateDate = DateTime.Now;
            Bill.SendDate = DateTime.Now;
            Bill.BillGoodsCount = 1;
            Bill.BillTagCount = Bill.BillGoodsCount;
            Bill.DeliveryService = false;
            return Bill;
        }

        private BillDataInfo GetBillDataSource(String recordId)
        {
            IDictionary<String, String> dictx;
            //构建初始对象
            BillDataInfo Bill = this.CreateBill();

            //填充初始化列表数据
            UserIdentity identity = GlobalObjects.CurrentUser;
            Bill.StartSiteName = identity.SiteName;
            Bill.StartSiteId = identity.SiteID;
            string start_siteID = identity.SiteID.Trim();
            //起始网点列表
            var dict = OutletsDAO.GetOutletsNamesBySiteId(start_siteID);
            Bill.StartOutletList = dict;
            Bill.StartOutletId = GlobalObjects.GSetting.MachineOutletId;
            //收货员列表
            dictx = UserDAO.GetUserBySiteId(identity.SiteID);
            Bill.ReceivingClerkList = dictx;
            //目的站点列表
            dictx = SitesDAO.GetDesSitesByStartSite(start_siteID);
            Bill.DesSiteList = dictx;
            //转寄地
            Bill.VAForwardAddrList = dictx;

            //继续初始化数据
            if (String.IsNullOrEmpty(recordId))
            {
                if (Bill.StartOutletList.Count > 0)
                    Bill.StartOutletId = dict.Keys.First();

                if (Bill.ReceivingClerkList.Count > 0)
                    Bill.ReceivingClerk = Bill.ReceivingClerkList.Keys.First();
                if (Bill.DesSiteList.Count > 0)
                {
                    String defaultStation = Bill.DesSiteList.Keys.First();
                    Bill.DesSiteId = defaultStation;
                    //根据目的站点获取目的网点
                    dictx = OutletsDAO.GetOutletsNamesBySiteId(defaultStation);
                    Bill.DesOutletList = dictx;
                    if (dictx.Count > 0)
                        Bill.DesOutletId = dictx.Keys.First();
                }
                ////新建运单号
                //String localMachineNo = GlobalObjects.GSetting.MachineID;
                //String BillCode = IdentityMrg.GetNewBillCode(identity.SiteID, Bill.StartOutletId, localMachineNo);
                //Bill.BillCode = BillCode;
            }
            else
            {
                //从数据库加载数据
                OWaybill OBill = WaybillDAO.GetBill(recordId);
                LoadBillDataInfo(Bill, OBill);

                Bill.BillTagCount = Bill.BillGoodsCount;

                //根据目的站点获取目的网点
                if (!String.IsNullOrWhiteSpace(Bill.DesSiteId))
                {
                    dictx = OutletsDAO.GetOutletsNamesBySiteId(Bill.DesSiteId);
                    Bill.DesOutletList = dictx;
                }
            }
            return Bill;
        }
        private BillDataInfo LoadBillDataInfo(BillDataInfo Bill, OWaybill model)
        {
            if (model == null) return Bill;
            Bill.BillCode = model.WaybillID;
            Bill.StartSiteId = model.Origin;//原寄地的站点
            Bill.StartOutletId = model.OriginNetWork;//原寄的网点
            Bill.DesSiteId = model.Destinations;//目的地站点
            Bill.DesOutletId = model.DestinationsNetwork;//目的网点

            if (model.CreatedDate != null && model.CreatedDate.HasValue)
                Bill.CreateDate = model.CreatedDate.Value;

            Bill.ReceiverPhone = model.ConsigneeTel;
            Bill.ReceiverCellPhone = model.ConsigneeMobile;
            Bill.ReceiverAddress = model.ConsigneeAddress;
            Bill.ReceiverName = model.Consigneeor;
            Bill.SendDate = model.ConsignorDate;
            Bill.SenderName = model.Consignor;
            Bill.SenderPhone = model.ConsignorTel;
            Bill.SenderCellPhone = model.ConsignorMobile;
            Bill.SenderAddress = model.ConsignorAddress;
            //Bill.ReceivingClerk = model.ConsignorDutyID;
            Bill.ReceivingClerk = model.Column3;
            Bill.VABankAccount = model.ConsignorCardID;
            Bill.VABank = model.ConsignorBank;       //银行名称
            Bill.BillGoodsName = model.GoodsName;
            Bill.BillGoodsCount = model.Numbers;
            Bill.BillPackageType = model.PackageType;

            #region 量方称重运费
            Bill.BillFreightFee = model.Freight;//运费
            Bill.BillFeeSRC = model.OriginAccept;//源站点收
            Bill.BillFeeDES = model.DestinationAccept;//目的站收

            Bill.BillGoodsWeight = model.Weight;
            Bill.BillGoodsLength = model.Longth;
            Bill.BillGoodsWidth = model.Width;
            Bill.BillGoodsHeight = model.Height;
            Bill.BillGoodsVolume = model.Volume;
            Bill.BillGoodsCode = model.GoodsCode;
            #endregion

            Bill.VAAgencyFund = model.CollectionFee;
            Bill.VAAgencyFundHandlingCharge = model.CollectionServicesFee;//代收款服务费  
            Bill.VAClaimedValue = model.InsuranceFee;//货物价值
            Bill.VAInsuranceFee = model.InsuredServicesFee;//保价费 
            Bill.VAInsuranceFeeWindow = model.Column1;  //保价费收费窗口
            Bill.VANoticeDelivery = model.NoteDelivery;
            Bill.VANoticDeliveryFee = model.NoteDeliveryFee;

            string strBacktype = model.BackType;
            if (!String.IsNullOrEmpty(strBacktype))//签回单类型
            {
                Bill.VAReceiptTypeReceipt = strBacktype.Contains(CONST_BackTypes[0]);
                Bill.VAReceiptTypeStamp = strBacktype.Contains(CONST_BackTypes[1]);
                Bill.VAReceiptTypeIDNum = strBacktype.Contains(CONST_BackTypes[2]);
            }

            Bill.VAAdvance = model.Advanced;
            Bill.VADeliveryFee = model.DeliveryCharges;
            Bill.VADeliveryFeeWindow = model.Column2;   //送货费收费窗口
            Bill.VAForwardFee = model.ForwardFee; //转寄费
            Bill.VAForwardAddr = model.ForwardAddress; //转寄地
            Bill.VACollaborativeLogistics = model.ForwardCompany;//合作物流

            Bill.DeliveryService = CONST_DeliveryServiceMode_Delivers == model.ServicesMode;

            Bill.PayType = model.PayMode;
            Bill.BillComment = model.Memo;
            Bill.VAPrepay = model.PreAcceptFee;
            return Bill;
        }

        #region 计算源站点收取费用与目的站点收取费用

        /**
         * 计算算法：
         * 1.总计算公式
         * 1)始发站收=始发站收①+始发站收②
         * 2)目的站收=目的站收①+目的站收②
         * 
         * 2.始发站收①，目的站收①（保价费，送货费）
         *  始发站收①=保价费，送货费中 指定收取的始发站收费用和
         *  目的站收①=保价费，送货费中 指定收取的目的站收费用和
         *  
         * 3.始发站收②，目的站收②（按提付方式计算）
         *  1)提付方式
         *      始发站收②=0
         *      目的站收②=运费+代收款+代收款手续费(0)+垫付+转寄费
         *  2)现付方式(或其他)
         *      始发站收②=运费+转寄费
         *      目的站收②=代收款+代收款手续费(0)+垫付
         * */
        private void CalcStartSiteTotalFee(BillDataInfo Bill)
        {
            int DeliveryFee = Bill.VADeliveryFeeWindow == CONST_Window_Station_SRC_KEY ? Bill.VADeliveryFee : 0;
            int InsuranceFee = Bill.VAInsuranceFeeWindow == CONST_Window_Station_SRC_KEY ? Bill.VAInsuranceFee : 0;
            int SRCSiteFee1 = DeliveryFee + InsuranceFee;
            int SRCSiteFee2 = 0;
            if (Bill.PayType == CONST_PAYMENT_Type_PAYLATER_KEY)//提付
            {
                SRCSiteFee2 = 0;
            }
            else//现付及其它
            {
                SRCSiteFee2 = Bill.BillFreightFee + Bill.VAForwardFee;//运费+转寄费
            }
            Bill.BillFeeSRC = SRCSiteFee1 + SRCSiteFee2;
        }
        private void GetEndSiteTotalFee(BillDataInfo Bill)
        {
            int DeliveryFee = Bill.VADeliveryFeeWindow == CONST_Window_Station_DES_KEY ? Bill.VADeliveryFee : 0;
            int InsuranceFee = Bill.VAInsuranceFeeWindow == CONST_Window_Station_DES_KEY ? Bill.VAInsuranceFee : 0;
            int DesSiteFee1 = DeliveryFee + InsuranceFee;
            int DesSiteFee2 = 0;
            if (Bill.PayType == CONST_PAYMENT_Type_PAYLATER_KEY)//提付
            {
                //运费+代收款+代收款手续费(0)+垫付+转寄费
                DesSiteFee2 = Bill.BillFreightFee + Bill.VAAgencyFund + Bill.VAAgencyFundHandlingCharge + Bill.VAAdvance + Bill.VAForwardFee;
            }
            else//现付及其它
            {
                //代收款+代收款手续费(0)+垫付
                DesSiteFee2 = Bill.VAAgencyFund + Bill.VAAgencyFundHandlingCharge + Bill.VAAdvance;
            }
            Bill.BillFeeDES = DesSiteFee1 + DesSiteFee2;
        }
        #endregion

    }
}
